/*
 * 文件名：UserDAOImpl.java
 * 版权：Copyright by www.wootide.com
 * 描述：用户DB层接口的实现类
 * 修改人：lizl
 * 修改时间：2012-9-13
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.manager.dao.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.wootide.cms.common.manager.GeneralUtils;
import com.wootide.cms.common.manager.SHException;
import com.wootide.cms.manager.dao.GenericDAO;
import com.wootide.cms.manager.dao.IUserDAO;
import com.wootide.cms.manager.pojo.User;


/**
 * 〈一句话功能简述〉：用户接口的实现类<br>
 * 〈功能详细描述〉
 * @author lizl
 * @version 2012-9-13
 * @see UserDAOImpl
 * @since
 */

public class UserDAOImpl extends GenericDAO<User, Long> implements IUserDAO
{
    /**
     * 日志处理
     */
    private static final Logger LOGGER = Logger.getLogger(UserDAOImpl.class);
    
    /** (non-Javadoc):对用户授权:与角色相关<br>
     *     1.即将某个用户与相应的角色进行关联，就可以拥有该角色对某个“资源”的操作权限，<br>
     * 这里对资源的操作权限规定为：增、删、改、查的顺序——》用四位二进制XXXX表示：<br>
     * 例如，1101——》增、删、查，没有修改操作权限；<br>
     *     2.具体实现：
     *       (1)获取用户、角色信息；
     *       (2)向用户角色关联表中插入一条记录：insert into tbl_user_role() values();<br>
     * @see com.wootide.smartclub.db.IUserDAO#grantUser(com.wootide.smartclub.base.pojo.User)
     */
    /*public void grantUser(User user, Role role) throws Exception
    {
        //第一步：判断当前该用户user是否已经拥有该角色role
        Set<Role> roles = this.get(user.getUserId()).getUserRoles();
        for (Role tr : roles)
        {
            if (!tr.getRoleId().equals(role.getRoleId()))
            {
                //第二步：如果没有授权，再进行相应授权操作；否则不操作
                user.getUserRoles().add(role);
                role.getRoleUsers().add(user);
                this.update(user);
            }
        }
        //这里用update,会出现多个对象与其关联的某个对象有关系（从而出现异常）；
        //this.update(user);
        getHibernateTemplate().merge(user);
    }*/
    
    /** (non-Javadoc):插入用户
     * @see com.wootide.smartclub.db.IUserDAO#insertUser(com.wootide.smartclub.base.pojo.User)
     */
    @Override
    public void insertUser(User user) throws Exception
    {
        try
        {
            this.save(user);
        }
        catch (Exception e)
        {
           e.printStackTrace();
        }
       
    }
    
    /** (non-Javadoc):根据用户ID查询
     *     1.将传入的对象进行解析，封装成SQL或者PLSQL语句，再与数据库进行交互，
     *     获取相应的数据记录集合；
     *     2.【查询约束条件】？？见server层
     * @see com.wootide.smartclub.db.IUserDAO#queryUserById(com.wootide.smartclub.base.pojo.User)
     */
    @Override
    public User queryUserById(User user) throws Exception
    {
        
        return this.get(user.getUserId());
    }
    
    /** (non-Javadoc):启用某个用户，就是修改该用户的状态属性userState = 0 表述启用;
     * @see com.wootide.smartclub.db.IUserDAO#startUser(com.wootide.smartclub.base.pojo.User)
     */
    @Override
    public void startUser(User user) throws Exception
    {
        this.getHibernateTemplate().update(user);
    }
    
    /** (non-Javadoc):停用某个用户，就是修改该用户的状态属性userState = 1 表述停用;
     * @see com.wootide.smartclub.db.IUserDAO#stopUser(com.wootide.smartclub.base.pojo.User)
     */
    @Override
    public void stopUser(User user) throws Exception
    {
        this.update(user);
    }
    
    /** (non-Javadoc):修改用户信息
     * @see com.wootide.smartclub.db.IUserDAO#updateUser(com.wootide.smartclub.base.pojo.User)
     */
    @Override
    public void updateUser(User user) throws Exception
    {
        this.update(user);
    }
    
    /** (non-Javadoc)：
     * Time:2012年9月18日16:48:22<br>
     * 根据查询条件查找用户,且支持【Hibernate分页查询】：<br>
     *     1.这里查询的条件有多个不同的参数，有的需要精确相等，有的需要<br>
     *     部分匹配；<br>
     *     2.【输入查询条件约束规定】这里暂定为用户属性：<br>
     *     userID/userName/userMale/userState/userJob<br>
     * @see com.wootide.smartclub.db.IUserDAO#queryUser(com.wootide.smartclub.base.pojo.User)
     */
    @Override
    public int queryUser(User user) throws Exception
    {
        Map<String, Object> paramMap = null;
        paramMap = parseToMap(user);
        return super.fuzzyListPojoCount(User.class, paramMap);
    }
    
    /**
     * 根据对象属性进行查询，返回单个对象
     */
    @Override
    public User getUser(User user) throws SHException
    {
        Map<String, Object> paramMap = null;
        paramMap = parseToMap(user);
        return (User)findPojoByAttr(User.class, paramMap);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<User> getUserList(User user, int start, int pageSize)
            throws SHException
    {
        Map<String, Object> paramMap = null;
        Map<String, String> orderMap = new LinkedHashMap<String, String>();
        List<User> userList = null;
        try
        {
            paramMap = parseToMap(user);
            userList = super.fuzzyListPojoByAttr(User.class,
                    paramMap,
                    orderMap,
                    start,
                    pageSize);
        }
        catch (Exception e)
        {
            LOGGER.error(e.getMessage());
        }
        return userList;
    }
    
    /**
     * 
     * Description: 将相应的object对象转化为Map对象<br>
     * Implement: <br>
     * @param user
     * @return
     * @see
     * @return Map<String,Object>
     * @author Administrator
     */
    private Map<String, Object> parseToMap(User user)
    {
        Map<String, Object> paramMap = new LinkedHashMap<String, Object>();
        if (GeneralUtils.isNotNull(user.getUserId()))
        {
            paramMap.put("userId", user.getUserId());
        }
        if (StringUtils.isNotEmpty(user.getUserName()))
        {
            paramMap.put("userName", user.getUserName());
        }
        if (StringUtils.isNotEmpty(user.getUserEmail()))
        {
            paramMap.put("userEmail", user.getUserEmail());
        }
        if (StringUtils.isNotEmpty(user.getUserMobile()))
        {
            paramMap.put("userMobile", user.getUserMobile());
        }
        if (StringUtils.isNotEmpty(user.getUserIdentityCard()))
        {
            paramMap.put("userIdentityCard", user.getUserIdentityCard());
        }
        return paramMap;
    }

    @Override
    public List<User> getAllUsers() throws SHException
    {
        // TODO Auto-generated method stub
        return super.list();
    }

}
